package com.ftw.commons.utils

import android.annotation.SuppressLint
import android.content.Context
import com.ftw.commons.R
import com.ftw.commons.log.LogUtils
import java.text.SimpleDateFormat
import java.util.*

/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2020/2/27
 */
object TimeUtils {
    /*
 * 将时间戳转换为时间
 */
    @SuppressLint("SimpleDateFormat")
    fun timestampConversion(context: Context, timeMillis: Long): String {

        val currentData = Calendar.getInstance()

        val currentYear = currentData.get(Calendar.YEAR)
        val currentDay = currentData.get(Calendar.DATE)
        val currentMonth = currentData.get(Calendar.MONTH)
        val currentHour = currentData.get(Calendar.HOUR_OF_DAY)
        val currentMinute = currentData.get(Calendar.MINUTE)

        currentData.time = Date(timeMillis)

        val remoteYear = currentData.get(Calendar.YEAR)
        val remoteDay = currentData.get(Calendar.DATE)
        val remoteMonth = currentData.get(Calendar.MONTH)
        val remoteHour = currentData.get(Calendar.HOUR_OF_DAY)
        val remoteMinute = currentData.get(Calendar.MINUTE)

        if (currentYear - remoteYear >= 1) {
            return SimpleDateFormat("yyyy-MM-dd HH:mm").format(currentData.time)
        }

        if (currentMonth - remoteMonth >= 1) {
            return SimpleDateFormat("MM-dd HH:mm").format(currentData.time)
        }

        if (currentDay - remoteDay > 1) {
            return SimpleDateFormat("MM-dd HH:mm").format(currentData.time)
        }


        if (currentDay - remoteDay == 1) {
            return context.getString(
                R.string.yesterday,
                SimpleDateFormat("HH:mm").format(currentData.time)
            )
        }

        if (currentHour - remoteHour >= 1) {
            return context.getString(
                R.string.nowadays,
                SimpleDateFormat("HH:mm").format(currentData.time)
            )
        }

        if (currentMinute - remoteMinute in 1..59) {
            return context.getString(
                R.string.minutes_ago,
                (currentMinute - remoteMinute).toString()
            )
        } else {
            return context.getString(R.string.just)
        }
    }

    fun isShowHideTime(time: Long, startTime: Long): Boolean {
        val interval = (time - startTime) / (1000 * 60)
        //5分钟以上
        return interval >= 5
    }

    @SuppressLint("SimpleDateFormat")
    fun getTime(date: Date): String {//可根据需要自行截取数据显示
        LogUtils.i("choice date millis: " + date.time)
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return format.format(date)
    }

    /*
 * 将时间戳转换为时间
 */
    @SuppressLint("SimpleDateFormat")
    fun stampToDate(timeMillis: Long): String? {
        val simpleDateFormat =
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val date = Date(timeMillis)
        return simpleDateFormat.format(date)
    }
    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    fun getOldDate(distanceDay: Int): Date {
        val dft = SimpleDateFormat("yyyy-MM-dd")
        val beginDate = Date()
        val date = Calendar.getInstance()
        date.time = beginDate
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay)
        return dft.parse(dft.format(date.time))
    }
    @SuppressLint("SimpleDateFormat")
    fun longToString(time: Long, format: String): String {
        val date = Date(time)
        val formatStr = SimpleDateFormat(format)
        return formatStr.format(date)
    }

    @SuppressLint("SimpleDateFormat")
    fun longSToString(time: Long): String {

        if (time < 60) {
            return time.toString() + "秒"
        } else if (time < 60 * 60) {
            return time.toString() + "小时"
        } else {
            return time.toString() + "天"
        }
    }


}